using WeatherMcpServer.API;
using WeatherMcpServer.API.Controllers;

var builder = WebApplication.CreateBuilder(args);

// 添加 HttpClient 并设置默认请求头
builder.Services.AddHttpClient<WeatherController>(client =>
{
    client.Timeout = TimeSpan.FromSeconds(20);
    try
    {
        client.DefaultRequestHeaders.UserAgent.ParseAdd("WeatherMcpServer/1.0");
    }
    catch { }
    try
    {
        client.DefaultRequestHeaders.Accept.ParseAdd("application/json");
    }
    catch { }
});

// 添加控制器支持
builder.Services.AddControllers();

// Swagger
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// 添加 CORS 支持
builder.Services.AddCors(options =>
{
    options.AddDefaultPolicy(policy =>
    {
        policy.AllowAnyOrigin()
              .AllowAnyMethod()
              .AllowAnyHeader();
    });
});

// 添加日志
builder.Services.AddLogging();

var app = builder.Build();

// 启用 Swagger（开发环境）
//if (app.Environment.IsDevelopment())
//{
    app.UseSwagger();
    app.UseSwaggerUI();
//}

// 启用 CORS
app.UseCors();

// 添加控制器路由
app.MapControllers();

// 健康检查
app.MapGet("/health", () => "OK");

// 根路径跳转到 Swagger
app.MapGet("/", () => Results.Redirect("/swagger"));

app.Run();
